﻿using AutoMapper;
using BCVP.Net8.IService;
using BCVP.Net8.Model;
using BCVP.Net8.Repository.Base;
using SqlSugar;
using System.Linq.Expressions;

namespace BCVP.Net8.Service
{
    public class BaseServices<TEntity, TVo> : IBaseServices<TEntity, TVo> where TEntity : class, new()
    {
        private readonly IMapper _mapper;
        private readonly IBaseRepository<TEntity> _baseRepository;

        public ISqlSugarClient Db => _baseRepository.Db;

        public BaseServices(IMapper mapper, IBaseRepository<TEntity> baseRepository)
        {
            _mapper = mapper;
            _baseRepository = baseRepository;
        }

        //public async Task<List<TEntity>> Query()
        //{
        //    var baseRepo = new BaseRepository<TEntity>();
        //    return await baseRepo.Query();
        //}

        //public async Task<List<TVo>> Query2()
        //{
        //    var baseRepo = new BaseRepository<TEntity>();
        //    var entities = await baseRepo.Query();
        //    var llout = _mapper.Map<List<TVo>>(entities);
        //    return llout;
        //}

        public async Task<List<TVo>> Query()
        {
            var entities = await _baseRepository.Query();
            //Console.WriteLine($"_baseRepository 实例HashCode ： {_baseRepository.GetHashCode()}");
            var llout = _mapper.Map<List<TVo>>(entities);
            return llout;
        }
        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity">博文实体类</param>
        /// <returns></returns>
        public async Task<long> Add(TEntity entity)
        {
            return await _baseRepository.Add(entity);
        }

        public async Task<List<TVo>> QuerySplit(Expression<Func<TEntity, bool>> whereExpression, string orderByFields = null)
        {
            return _mapper.Map<List<TVo>>(await _baseRepository.QuerySplit(whereExpression, orderByFields));
        }

        public async Task<List<long>> AddSplit(TEntity entity)
        {
            return await _baseRepository.AddSplit(entity);
        }

    }
}
